<template>
    <div class="audit_manage">
        <!--信审权限管理-->
        <div class="search" v-show="showAuditorSearch">
            <el-form ref="form" :inline="true" :model="form" :rules="rules">
                <el-form-item prop="accountName" :label="$t('common.accountName')"><!--账号名称-->
                    <select-account-list :type="selectDataType" v-model="form.accountName" :placeholder="$t('verify.plsInput')"></select-account-list>
                </el-form-item>
                <el-form-item v-if="!isCaptain" prop="roleType" :label="$t('common.roleType')"><!--角色类型-->
                    <el-select v-model.number="form.roleType" @change="roleTypeChange">
                        <el-option v-for="auditor in auditorTypeList" :key="auditor.id" :value="auditor.id" :label="$root.local=='zhCN'?auditor.name:auditor.idName"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item prop="isDelete" :label="$t('authority.accountStatus')">
                    <el-select v-model="form.isDelete" clearable :placeholder="$t('verify.plsChoose')" style="width: 100%"><!--账号状态-->
                        <el-option key="0" :label="$t('collection.activated')" :value="0"></el-option>
                        <el-option key="1" :label="$t('collection.closed')" :value="1"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSearch" icon="search">{{$t('common.search')}}</el-button>
                </el-form-item>
            </el-form><!--end form-->
        </div>

        <div class="data_list_panel" :style="listPanelStyle">
            <waiting-list
                :active-tab="activeTab"
                :search-form="form"
                @search="hasSearch"
                :auditor-type-list="auditorTypeList"
                @getData="getData"
                v-if="activeTab === 'waitingList'">
            </waiting-list>

            <first-audit
                :active-tab="activeTab"
                :search-form="form"
                @search="hasSearch"
                :is-captain="isCaptain"
                @getData="getData"
                v-if="activeTab === 'firstAudit'">初审
            </first-audit>

            <last-audit
                :active-tab="activeTab"
                :search-form="form"
                @search="hasSearch"
                @getData="getData"
                v-if="activeTab === 'lastAudit'">终审
            </last-audit>

            <captain
                :active-tab="activeTab"
                :search-form="form"
                @search="hasSearch"
                v-if="activeTab === 'captain'">信审组长
            </captain>

            <leader
                :active-tab="activeTab"
                :search-form="form"
                @search="hasSearch"
                v-if="activeTab === 'leader'">信审主管
            </leader>

            <order-first-audit
                :active-tab="activeTab"
                :search-form="form"
                @search="hasSearch"
                @updateActiveTab="updateActiveTab"
                v-if="activeTab === 'orderFirstAudit'">初审订单
            </order-first-audit>

            <order-last-audit
                :active-tab="activeTab"
                :search-form="form"
                @search="hasSearch"
                @updateActiveTab="updateActiveTab"
                v-if="activeTab === 'orderLastAudit'">终审订单
            </order-last-audit>
        </div>
    </div>
</template>

<script>
    import Captain from './captain';
    import Leader from './leader';
    import FirstAudit from './first-audit';
    import LastAudit from './last-audit';
    import OrderFirstAudit from './order-first-audit';
    import OrderLastAudit from './order-last-audit';
    import WaitingList from './waiting-list';
    import SelectAccountList from '@/components/select-account-list';

    export default {
        name: 'auditManage',
        components: {
            Captain,
            Leader,
            FirstAudit,
            LastAudit,
            OrderFirstAudit,
            OrderLastAudit,
            WaitingList,
            SelectAccountList
        },
        data() {
            return {
                form: {
                    auditorId: '',
                    leaderId: '',
                    accountName: '',
                    isDelete: '',
                    roleType: 3,
                    search: false
                },
                rules: {
                    accountName: [],
                    roleType: []
                },
                auditorTypeList: [],
                activeTab: '',
                captainList: [],
                isCaptain: true
            };
        },
        computed: {
            listPanelStyle() {
                const style = {};
                if (this.activeTab === 'orderFirstAudit' || this.activeTab === 'orderLastAudit') {
                    style.margin = '0';
                }
                return style;
            },
            showAuditorSearch() {
                return this.activeTab !== 'orderFirstAudit' && this.activeTab !== 'orderLastAudit';
            },
            selectDataType() {
                return {
                    systemType: 'audit',
                    roleType: this.form.roleType,
                    leaderId: this.form.leaderId
                }
            }
        },
        methods: {
            getAuditType() {
                this.$api.authorityManage.getAllAuditorType().then((res) => {
                    const {status, data, error} = res;
                    if ('1' === status && '00000000' === error) {
                        this.auditorTypeList = data;
                        this.auditorTypeList.forEach((item, index) => {
                            // 去除信审主管
                            // if (item.id === 6) {
                            //     this.auditorTypeList.splice(index, 1);
                            // }
                        });
                        this.auditorTypeList.push({id: 0, name: '待分配', idName: this.$t('tel.waitAssign')});
                        // console.log(this.$root.local);
                    }
                });
            },
            onSearch() {
                this.form.accountName = this.form.accountName.trim();
                if (this.form.accountName || this.form.isDelete + '') {
                    this.form.search = true;
                }
            },
            hasSearch(val) {
                this.form.search = false;
            },
            roleTypeChange(val) {
                this.form.accountName = '';
                this.form.isDelete = '';
                switch (+val) {
                    case 0:
                        this.activeTab = 'waitingList';
                        break;
                    case 1:
                        this.activeTab = 'firstAudit';
                        break;
                    case 2:
                        this.activeTab = 'lastAudit';
                        break;
                    case 3:
                        this.activeTab = 'captain';
                        break;
                    case 6:
                        this.activeTab = 'leader';
                        break;
                    default:
                        this.activeTab = 'captain';
                }
            },
            getData(form, type) {
                // console.log(form);
                this.form = Object.assign(this.form, form);
                this.activeTab = type;
            },
            getCaptainList() {
                const params = {
                    type: 3,
                    name: '',
                    leaderId: ''
                };
                // this.tableData = [];
                this.$api.authorityManage.getAllAuditorInfo(params).then((res) => {
                    const {status, data, error} = res;
                    if ('1' === status && '00000000' === error && data.data) {
                        this.captainList = data.data;
                        const user = this.$storage.get('user');
                        this.isCaptain = false;
                        this.captainList.forEach((item) => {
                            if (item.name === user.username) {
                                this.isCaptain = true;
                                this.form.roleType = 1;
                                this.form.leaderId = item.auditorId;
                                this.activeTab = 'firstAudit';
                            }
                        });
                        if (!this.isCaptain) {
                            this.activeTab = 'captain';
                        }
                    }
                });
            },
            updateActiveTab(val) {
                if (val) {
                    this.activeTab = val;
                    this.form.accountName = '';
                    this.form.isDelete = '';
                    this.$nextTick(() => {
                        this.form.roleType = +this.form.roleType;
                    });
                }
            }
        },
        created() {
            this.getCaptainList();
            this.getAuditType();
        }
    };
</script>

<style lang="scss" scoped>
    .inputPhone {
        width: 265px;
    }

    .search {
        background-color: #fff;
        padding: 22px 40px 0;
    }
</style>
